<?php

/**
 * @category   Oos
 * @package    Oos_Site
 * @license    New BSD License
 */

/**
 * Zend_Gdata_Query
 */
require_once('Zend/Gdata/Query.php');

class Oos_Site_SiteQuery extends Zend_Gdata_Query
{
	protected $_defaultFeedUri = 'http://11870.com/api/v1/search';
	protected $_queryText = null;
	protected $_tags = null;
	protected $_tagop = 'and';
	protected $_swpoint = null;
	protected $_nepoint = null;
	protected $_centerpoint = null;
	protected $_radius = null;
	protected $_areaSlug = null;
	protected $_lang = null;
	protected $_entrypoint = null;
	protected $_apiKey = null;
	protected $_authSign = null;
	protected $_user_slug = null;
	protected $_location = null;
	protected $_scope = null;
	
	public function __construct($entrypoint)
	{
		parent::__construct();
		$this->_entrypoint = $entrypoint;
	}

	public function setUserSlug($value) {
		$this->_user_slug = $value;
		return $this;
	}

	public function setApiKey($value) {
		$this->_apiKey = $value;
		return $this;
	}

	public function setAuthSign($value) {
		$this->_authSign = $value;
		return $this;
	}

	public function setTagOp($value) {
		if ($value != 'and' && $value != 'or')
			throw new Zend_Exception('Tag operator can only be "or"/"and"');
			
		$this->_tagop = $value;
		return $this;
	}

	public function setLang($value) {
		$this->_lang = value;
		return $this;
	}

	public function setLocation($value) {
		$this->_location = $value;
		return $this;
	}

	public function setAreaType($value) {
		$this->_areaType = $value;
		return $this;
	}

	public function setAreaSlug($value) {
		$this->_areaSlug = $value;
		return $this;
	}

	public function setNEPoint($value) {
		$this->_nepoint = $value;
		return $this;
	}

	public function setSWPoint($value) {
		$this->_swpoint = $value;
		return $this;
	}

	public function setTags($value) {
		$this->_tags = $value;
		return $this;
	}

	public function setQueryText($value) {
		$this->_queryText = $value;
		return $this;
	}

	public function setCenterPoint($value) {
		$this->_centerpoint = $value;
		return $this;
	}

	public function setRadius($value) {
		$this->_radius = $value;
		return $this;
	}
	
	/**
	 * Scope can be 'global' (default), 'user', or 'contacts'
	 */
	public function setScope($value) {
		$this->_scope = $value;
		return $this;
	}

	public function getQueryUrl() {
		$uri = $this->_defaultFeedUri;

		if ($this->_areaSlug != null) {
			$this->_params['as'] = $this->_areaSlug;
		}

		if ($this->_swpoint != null && $this->_nepoint != null) {
			$sw = split(",", $this->_swpoint);
			$ne = split(",", $this->_nepoint);
			$this->_params['bbox'] = $sw[1] . "," . $sw[0] . "," . $ne[1] . "," . $ne[0];
		}

		if ($this->_centerpoint != null && $this->_radius != null) {
			$coords = split(",", $this->_centerpoint);

			$this->_params['lat'] = $coords[0];
			$this->_params['lon'] = $coords[1];
			$this->_params['radius'] = $this->_radius;
		}

		if ($this->_location != null) {
			$this->_params['l'] = $this->_location;
		}

		if ($this->_queryText != null) {
			$this->_params['q'] = $this->_queryText;
		}

		if ($this->_scope != null) {
			$this->_params['scope'] = $this->_scope;
		}

		if ($this->_tagop != null) {
			$this->_params['tagop'] = $this->_tagop;
		}
		
		if($this->_apiKey)
		{
			$this->_params['appToken'] = $this->_apiKey;
		} else {
			require_once 'Zend/Gdata/App/AuthException.php';
            throw new Zend_Gdata_App_AuthException('No API key set');
		}
		
		if($this->_authSign)
		{
			$this->_params['authSign'] = $this->_authSign;
		} else {
			require_once 'Zend/Gdata/App/AuthException.php';
            throw new Zend_Gdata_App_AuthException('Not authentication signature set');
		}

		$uri .= $this->getQueryString();

		$tagarray = array();

		foreach ($this->_tags as $tag) {
			$tagarray[] = 'tag=' . urlencode($tag);
		}

		$uri .= '&' . implode('&', $tagarray);

		return $uri;
	}

	public function getQueryString() {
		return parent::getQueryString();
	}
}


?>
